<?php

namespace app\merchant\controller\wxapp;

use app\merchant\controller\Base;
use app\merchant\model\Category as CategoryModel;
use app\merchant\model\sharing\Category as SharingCategoryModel;
use app\merchant\model\article\Category as ArticleCategoryModel;
use app\merchant\model\WxappPage as WxappPageModel;
use app\merchant\model\WxappCategory as WxappCategoryModel;

/**
 * 小程序页面管理
 * Class Page
 * @package app\merchant\controller\wxapp
 */
class Page extends Base
{
    /**
     * 页面列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new WxappPageModel;
        $list = $model->getList();
        return $this->renderSuccess($list,'查询成功');
    }

    /**
     * 新增页面获取详情
     * @return array|mixed
     */
    public function addData()
    {
        $model = new WxappPageModel;
        $data = [
            'defaultData'=>$model->getDefaultItems(),
            'jsonData'=> ['page' => $model->getDefaultPage(), 'items' => []],
            'opts'=>[
                'catgory' => CategoryModel::getCacheTree(),
                'sharingCatgory' => SharingCategoryModel::getCacheTree(),
                'articleCatgory' => ArticleCategoryModel::getALL(),
            ],
        ];
        return $this->renderSuccess($data,"查询成功");

    }


    /**
     * 新增页面
     * @return array|mixed
     */
    public function add()
    {
        $model = new WxappPageModel;
        // 接收post数据
        $post = $this->request->post('data', null, null);
        if (!$model->add(json_decode($post, true))) {
            return $this->renderError([],'添加失败');
        }
        return $this->renderSuccess([],'添加成功');
    }


    /**
     * 编辑页面数据
     * @param $page_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['page_id']) || isInt($param['page_id'])){
            return $this->renderError([],"参数错误");
        }
        // 页面详情
        $model = WxappPageModel::detail($param['page_id']);
        if(!$model){
            return $this->renderError([],"该页面不存在，请刷新重试");
        }

        $data = [
            'defaultData'=>$model->getDefaultItems(),
            'jsonData'=>$model['page_data'],
            'opts'=>[
                'catgory' => CategoryModel::getCacheTree(),
                'sharingCatgory' => SharingCategoryModel::getCacheTree(),
                'articleCatgory' => ArticleCategoryModel::getALL(),
            ],
        ];
        return $this->renderSuccess($data,"查询成功");


    }


    /**
     * 编辑页面数据
     * @param $page_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->request->post();
        if(!isset($param['page_id']) || isInt($param['page_id'])){
            return $this->renderError([],"参数错误");
        }
        // 页面详情
        $model = WxappPageModel::detail($param['page_id']);
        if(!$model){
            return $this->renderError([],"该页面不存在，请刷新重试");
        }

        // 接收post数据
        $post = $this->request->post('data', null, null);
        if (!$model->edit(json_decode($post, true))) {
            return $this->renderError([],'更新失败');
        }
        return $this->renderSuccess([],'更新成功');

    }




    /**
     * 删除页面
     * @param $page_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        if(!isset($param['page_id']) || isInt($param['page_id'])){
            return $this->renderError([],"参数错误");
        }
        // 页面详情
        $model = WxappPageModel::detail($param['page_id']);
        if(!$model){
            return $this->renderError([],"该页面不存在，请刷新重试");
        }
        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

    /**
     * 设置默认首页
     * @param $page_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function setHome()
    {
        $param = $this->postData();
        if(!isset($param['page_id']) || isInt($param['page_id'])){
            return $this->renderError([],"参数错误");
        }
        // 页面详情
        $model = WxappPageModel::detail($param['page_id']);
        if(!$model){
            return $this->renderError([],"该页面不存在，请刷新重试");
        }
        // 帮助详情
        $model = WxappPageModel::detail($param['page_id']);
        if (!$model->setHome()) {
            return $this->renderError([],$model->getError() ?: '设置失败');
        }
        return $this->renderSuccess([],'设置成功');
    }


    /**
     * 分类模板
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function category()
    {
        $model = WxappCategoryModel::detail();

        return $this->renderSuccess($model,'查询成功');
    }

    /**
     * 分类模板提交
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function setCategory()
    {
        $param = $this->postData();
        if(!isset($param['category_style']) || isInt($param['category_style'])){
            return $this->renderError([],"请选择分类页样式");
        }
        $model = WxappCategoryModel::detail();

        if ($model->edit($param)) {
            return $this->renderSuccess([],'更新成功');
        }
        return $this->renderError([],$model->getError() ?: '更新失败');


    }


    /**
     * 页面链接
     * @return mixed
     */
    public function links()
    {
        return $this->renderSuccess([],"查询成功");
    }

}
